// خەت نۇسخىسىنى قوشۇش
function addFontLink(fontName) {
  // خەت نۇسخىلىرى fonts.css دىن ئوقۇلىدۇ
  return;
}

// RTL ۋە خەت نۇسخىسىنى قوللىنىش
function applyStyles(container, rtlEnabled, fontName, justifyEnabled, fontSize) {
  if (container) {
    // ئانا div نى تېپىش
    const markdownContainer = container.closest('.ds-markdown');
    const paragraphs = markdownContainer ? markdownContainer.querySelectorAll('p') : [];
    
    if (rtlEnabled) {
      container.style.direction = 'rtl';
    } else {
      container.style.direction = '';
    }
    
    if (fontName) {
      container.style.fontFamily = `'${fontName}', sans-serif`;
      // بارلىق p ئېلېمېنتلارغا خەت نۇسخىسىنى قوللىنىش
      paragraphs.forEach(p => {
        p.style.fontFamily = `'${fontName}', sans-serif`;
      });
    } else {
      container.style.fontFamily = '';
      paragraphs.forEach(p => {
        p.style.fontFamily = '';
      });
    }
    
    if (fontSize) {
      container.style.fontSize = fontSize;
      // بارلىق p ئېلېمېنتلارغا خەت چوڭلۇقىنى قوللىنىش
      paragraphs.forEach(p => {
        p.style.fontSize = fontSize;
      });
    } else {
      container.style.fontSize = '';
      paragraphs.forEach(p => {
        p.style.fontSize = '';
      });
    }
    
    if (!container.classList.contains('md-code-block')) {
      if (justifyEnabled) {
        // تېكىست بار div نىڭ ئانا div ىغا text-align قوشۇش
        const textContainer = container.closest('.markdown-body') || container;
        textContainer.style.textAlign = 'justify';
        // سۆز ئارىلىقىنى تەڭشەش
        textContainer.style.wordSpacing = '0.1em';
        // بارلىق p ئېلېمېنتلارغا text-align قوللىنىش
        paragraphs.forEach(p => {
          p.style.textAlign = 'justify';
          p.style.wordSpacing = '0.1em';
        });
      } else {
        const textContainer = container.closest('.markdown-body') || container;
        textContainer.style.textAlign = '';
        textContainer.style.wordSpacing = '';
        paragraphs.forEach(p => {
          p.style.textAlign = '';
          p.style.wordSpacing = '';
        });
      }
    }
  }
}

// ھالەتنى تەكشۈرۈش ۋە قوللىنىش
function checkAndApplySettings() {
  chrome.storage.local.get(['rtlEnabled', 'selectedFont', 'justifyEnabled', 'fontSize'], function(result) {
    const parentXPath = "/html/body/div[1]/div/div[2]/div[2]/div/div[2]/div/div/div[1]";
    const parentContainer = document.evaluate(
      parentXPath,
      document,
      null,
      XPathResult.FIRST_ORDERED_NODE_TYPE,
      null
    ).singleNodeValue;

    const inputContainer = document.getElementById('chat-input');

    if (parentContainer) {
      const textDivs = parentContainer.querySelectorAll('div > div');
      textDivs.forEach(div => {
        if (div.textContent.trim()) {
          if (div.classList.contains('md-code-block')) {
            div.style.direction = 'ltr';
          } else {
            applyStyles(div, result.rtlEnabled, result.selectedFont, result.justifyEnabled, result.fontSize);
          }
        }
      });
    }

    if (inputContainer) {
      applyStyles(inputContainer, result.rtlEnabled, result.selectedFont, result.justifyEnabled, result.fontSize);
    }

    // خەت نۇسخىسىنى قوشۇش
    addFontLink(result.selectedFont);
  });
}

// خەۋەرلەرنى كۆزىتىش
chrome.runtime.onMessage.addListener(function(request, sender, sendResponse) {
  if (request.action === 'toggleRTL' || request.action === 'changeFont' || 
      request.action === 'toggleJustify' || request.action === 'changeFontSize') {
    checkAndApplySettings();
  }
});

document.addEventListener('DOMContentLoaded', checkAndApplySettings);

const observer = new MutationObserver(checkAndApplySettings);
observer.observe(document.body, {
  childList: true,
  subtree: true
}); 